<?php
/**
 * 功能: 商品收藏
 * User: 文丹
 * Date: 2018/8/3 0003
 * Time: 16:11
 */

namespace app\index\model;
use think\Db;
use think\Model;

class Collect extends Model{
    /**
     * 用户收藏记录查找
     * @param $user_id
     * @param $goods_id
     */
    public function checkBrowse($user_id,$goods_id){
        $result = Db::name('collect')
            ->where('user_id',$user_id)
            ->where('goods_id',$goods_id)
            ->find();
        return $result;

    }

    /**
     * 商品收藏
     * @param $data
     * @return int|string
     */
    public function add($data){
        $result = Db::name('collect')->insert($data);
        return $result;

    }

    /**
     * 展示收藏记录
     * @param $user_id
     * @return false|mixed|\PDOStatement|string|\think\Collection
     */
    public function getCollect($user_id,$type){
        $result = Db::name('collect')
            ->where('user_id',$user_id)
            ->where('type',$type)
            ->order('id desc')
            ->field('*,id as ids')
            ->select();
        $arr = [];
        if($result){
            foreach($result as $k=>$v){
                $arr[$k] = $v;
                $goods = Db::name('goods')->where('goods_id',$v['goods_id'])->find();
                if($goods){
                    if($type==2){
                        $arr[$k]['coupon_start_time'] = $goods['coupon_start_time'];
                        $arr[$k]['coupon_end_time'] = $goods['coupon_end_time'];
                        $arr[$k]['coupon_info'] = $goods['coupon_info'];
                    }
                    $arr[$k]['goods_exist'] = 1;
                }else{
                    if($type==2){
                        $arr[$k]['coupon_start_time'] = 0;
                        $arr[$k]['coupon_end_time'] = 0;
                        $arr[$k]['coupon_info'] = '';
                    }
                    $arr[$k]['goods_exist'] = 0;
                }


            }
        }

        return $arr;
    }

}